<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <!--头像-->
        <img src="../assets/logo.png" alt />
      </div>
      <!--添加表单-->
      <el-form ref="loginFormRef" :model="loginForm"
               :rules="loginRules" class="login_form"
               label-width="0px">

        <el-form-item prop="username">
          <!-- 用户名 -->
          <el-input v-model="loginForm.username"
                    prefix-icon="iconfont icondenglu"
                    placeholder="请输入用户名">
          </el-input>

        </el-form-item>

        <el-form-item prop="password">
          <!-- 密码 -->
          <el-input v-model="loginForm.password"
                    prefix-icon="iconfont iconmima"
                    type="text" placeholder="请输入密码">
          </el-input>
        </el-form-item>

        <el-form-item class="btns">
          <!-- 按钮 -->
          <el-button type="primary" @click="login('loginFormRef')">登录</el-button>
          <el-button type="info" @click="resetForm('loginFormRef')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: ""
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 8, message: "密码为 6~8 位", trigger: "blur" }
        ]
      }
    };
  },
    methods:{
      //登录
      login:function (formName) {
          let that = this;
          this.$refs[formName].validate((valid) => {
              if (valid) {  //判断表单信息是否通过验证  通过为true
                this.$axios.post("http://localhost:9000/user/login",that.loginForm).then((response) =>{
                    if (response.data.username != null){
                        this.$message({
                            message: '登录成功！！！',
                            type: 'success'
                        });
                        window.sessionStorage.setItem("user",response.data)
                        that.$router.push("/Home")   //路由跳转
                    }else {
                        this.$message.error("用户密码输入错误！！！")
                    }
                })
              }
          });
      },


        // 重置表单信息
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
};
</script>




<style lang="less" scoped>

// 跟节点样式
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
// 登录框
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;// 圆角
  position: absolute;// 绝对定位
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);// 根据自己位置 以自己为长度位移
  // 头像框
  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%; // 加圆角
    padding: 10px;
    box-shadow: 0 0 10px #ddd;// 盒子阴影
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #0ee;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%; // 加圆角
      background-color: #eee;
    }
  }
  .btns {
    display: flex;// 弹性布局
    justify-content: flex-end; // 尾部对齐
  }
  .login_form {
    position: absolute;
    bottom: 0%;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;// 设置边框
  }
}
</style>